package com.antilia.infomaresme.data.dao.hibernate;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import com.antilia.infomaresme.data.dao.interfaces.IItemEventsDao;
import com.antilia.infomaresme.data.dataobjects.EventStatus;
import com.antilia.infomaresme.data.dataobjects.Item;
import com.antilia.infomaresme.data.dataobjects.ItemEvent;
import com.antilia.infomaresme.data.dataobjects.ItemEventSubject;
/**
 * @author Ernesto
 *
 */
public class ItemEventsDaoHibernateImp extends AbstractDaoHibernateImpl<ItemEvent> implements IItemEventsDao {

	public ItemEventsDaoHibernateImp()
	{
		super(ItemEvent.class);
	}
	
	
	public void registerEvent(Item item, ItemEventSubject eventSubject, String oldValue, String newValue) {
		ItemEvent event = new ItemEvent();
		event.setItem(item);
		event.setStatus(EventStatus.open);
		event.setSubject(eventSubject);
		event.setOldValue(oldValue);
		event.setNewValue(newValue);
		event.setDate(new Date());
		save(event);
	}
	
	@SuppressWarnings("unchecked")
	public List<ItemEvent> getEventsAfter(Date date) {
		Criteria criteria = getSession().get().createCriteria(ItemEvent.class);
		criteria.add(Restrictions.ge("date", date));
		criteria.add(Restrictions.ge("status", EventStatus.open));
		return (List<ItemEvent>)criteria.list();
	}
}
